package com.sbtr.controller.feature;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sbtr.business.feature.dto.FeDatafileDto;
import com.sbtr.business.feature.dto.FeDatafileUploadDto;
import com.sbtr.business.feature.entity.FeDatafield;
import com.sbtr.business.feature.entity.FeDatafile;
import com.sbtr.business.feature.service.FeDatafieldService;
import com.sbtr.business.feature.service.FeDatafileService;
import com.sbtr.common.LoginConstants;
import com.sbtr.common.PageDto;
import com.sbtr.common.Response;
import com.sbtr.controller.common.CommonController;
import com.sbtr.exception.BDException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import okhttp3.*;
import okhttp3.RequestBody;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Base64Utils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 特征工程原始数据文件字段列表 前端控制器
 *
 * @author dgl
 * @since 2023-03-17
 */
@RestController
@RequestMapping("/feDatafile")
@Api(tags = "特征工程原始数据文件字段列表")
public class FeDatafileController extends CommonController<FeDatafile, FeDatafileService> {

    @Autowired
    FeDatafieldService feDatafieldService;

    @GetMapping("/pageList")
    @ApiOperation(httpMethod = "GET", value = "特征工程原始数据文件字段列表列表")
    public Response<PageDto<FeDatafile>> pageList(FeDatafileDto feDatafileDto){
        return Response.success(u.pageList(feDatafileDto));
    }

    @RequestMapping("/uploadAnalyze")
    @ApiOperation(httpMethod = "POST", value = "上传文件并分析")
    public Response<Map<String,Object>> uploadAnalyze(FeDatafileUploadDto feDatafileUploadDto, @RequestPart(value = "file") MultipartFile file, HttpServletRequest request){
        String username = request.getParameter(LoginConstants.REQUEST_PARAM_USERNAME);
        Map<String,Object> r = u.uploadAnalyze(feDatafileUploadDto,file,username);
        if(r.size() !=0){
            return Response.success(r);
        }else throw new BDException("解析为空");
    }

//    @RequestMapping("/test")
//    @ApiOperation(httpMethod = "POST", value = "测")
//    public void test(FeDatafileUploadDto feDatafileUploadDto, @RequestPart(value = "file") MultipartFile file, HttpServletRequest request) throws IOException {
//        OkHttpClient okHttpClient = new OkHttpClient();
//        String url = "http://127.0.0.1:61100/aiplatform/feDatafile/uploadAnalyze?processingId="+feDatafileUploadDto.getProcessingId();
//        RequestBody formBody = new MultipartBody.Builder().addFormDataPart("file",file.getOriginalFilename(),
//                RequestBody.create(MediaType.parse("application/octet-stream"),file.getBytes())).build();
//        Request request2 = new Request.Builder().url(url).post(formBody).build();
//        okhttp3.Response response = okHttpClient.newCall(request2).execute();
//        System.out.println(response.body().toString());
//    }

    @PostMapping("/analyzeSave")
    @ApiOperation(httpMethod = "POST", value = "分析保存,传文件id，在读取返回的datafileId，任意取一列的即可")
    public Response<FeDatafile> analyzeSave(Long id){
        FeDatafile feDatafile = u.analyzeSave(id);
        if(feDatafile.getId()!=null){
            return Response.success(feDatafile);
        }else throw new BDException("上传失败");
    }

    @GetMapping("/getFieldTreeByProcessId")
    @ApiOperation(httpMethod = "GET", value = "根据算子id获取字段属性列表")
    public Response<List<FeDatafield>> getFieldTreeByProcessId(FeDatafile feDatafile,String types){
        QueryWrapper<FeDatafile> wrapper =  new QueryWrapper<>(feDatafile);
        wrapper.last(" limit 1");
        FeDatafile feDatafile2 = u.getOne(wrapper);

        if(feDatafile2==null){
            return Response.success(null);
        }

        QueryWrapper<FeDatafield> wrapper2 =  new QueryWrapper<>();
        wrapper2.eq("datafile_id",feDatafile2.getId());
        if(StringUtils.isNotBlank(types)){
            String[] typeList = types.split(",");
            if(typeList.length!=0){
                wrapper2.in("type",typeList);
            }
        }

        return Response.success(feDatafieldService.list(wrapper2));
    }

    @GetMapping("/getTreeByProcessId")
    @ApiOperation(httpMethod = "GET", value = "根据算子id获取文件信息列表")
    public Response<Map<String,Object>> getTreeByProcessId(Long processingId){
        return Response.success(u.getTreeByProcessId(processingId));
    }

    @PostMapping("/execSQL")
    @ApiOperation(httpMethod = "POST", value = "执行sql文件")
    public void execSQL(@RequestPart(value = "file") MultipartFile file){
        u.execSQL(file);
    }

//    @PostConstruct
//    public void init(){
//        u.execSQL2("/data/service/aiplaform_new.sql");
//    }

}

